﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using IcarusProject.Tiles;
using IcarusProject.MazeGen;
using Microsoft.Xna.Framework.Input;

namespace IcarusProject.Icarus
{
    public class IcarusGame : DrawableGameComponent
    {
        #region Attributs
        private SpriteBatch _spriteBatch;
        private Texture2D _square;
        private Maze _labyrinthe;
        private Vector2 _playerPosition;

        #endregion

        #region Constructeur
        public IcarusGame(Game game) : base(game)
        {
            _labyrinthe = new Maze(12,7);
            _labyrinthe.Init().Build().FillMap(Tileset.GetSingleton().FloorID,
                                               Tileset.GetSingleton().HWallId,
                                               Tileset.GetSingleton().VWallId,
                                               Tileset.GetSingleton().CornerId);
            _playerPosition = Vector2.Zero;
        }
        #endregion

        #region Proprietes
        #endregion

        #region Methodes
        public override void Initialize()
        {
            base.Initialize();

            _spriteBatch = new SpriteBatch(GraphicsDevice);

        }

        protected override void LoadContent()
        {
            base.LoadContent();

            _square = Game.Content.Load<Texture2D>("sprites/square");
        }

        public override void Update(GameTime gameTime)
        {
            GameController gc = GameController.GetSingleton();

            if (gc.IsKeyDown(Keys.Left))
                _playerPosition.X -= 2f;
            if (gc.IsKeyDown(Keys.Right))
                _playerPosition.X += 2f;
            if (gc.IsKeyDown(Keys.Up))
                _playerPosition.Y -= 2f;
            if (gc.IsKeyDown(Keys.Down))
                _playerPosition.Y += 2f;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            Point origine = new Point(5, 5);
            int tileSize = 32;
            Color couleur = Color.White;

            _spriteBatch.Begin();

            for (int i = 0; i < _labyrinthe.MapWidth; ++i)
                for (int j = 0; j < _labyrinthe.MapHeight; ++j)
                {
                    _spriteBatch.Draw(_square,
                                      new Rectangle(origine.X + tileSize * i,
                                                    origine.Y + tileSize * j,
                                                    tileSize,
                                                    tileSize),
                                      Tileset.GetSingleton()[_labyrinthe[i, j]].ColorEffect);
                    _spriteBatch.Draw(_square,
                                      new Rectangle((int)_playerPosition.X,
                                                    (int)_playerPosition.Y,
                                                    tileSize / 2,
                                                    tileSize / 2),
                                      Color.Blue);
                }

                    _spriteBatch.End();
        }

        #endregion
    }
}
